package com.voute.View.Activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.Api.PersonInfoService;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Action.ExitAppAction;
import com.voute.Model.Action.StartRefreshTagsAction;
import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.FeedCirclePushMessageEntity;
import com.voute.R;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.PreferenceUtils;
import com.voute.Utils.RxBus;
import com.voute.Utils.ServiceFactory;
import com.voute.Utils.TransformUtils;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.event.EventBus;

/**
 * 摄者中心
 * Created by liucxu on 2017/1/21.
 */

public class SettingActivity extends BaseActivity {
    private RelativeLayout back_layout;
    private TextView clear_cache;
    private TextView about;
    private TextView exit;
    private TextView user_licence;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void setupView() {
        back_layout = $(R.id.back_layout);
        clear_cache = $(R.id.clear_cache);
        about = $(R.id.about);
        exit=$(R.id.exit);
        user_licence=$(R.id.user_licence);
        user_licence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(SettingActivity.this, WebViewActivity.class);
                intent.putExtra("webview_title","用户协议");
                intent.putExtra("webview_url","https://5miaoapp.com/eula");
                startActivity(intent);
            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(SettingActivity.this, WebViewActivity.class);
                intent.putExtra("webview_title","关于");
                intent.putExtra("webview_url","https://5miaoapp.com/about");
                startActivity(intent);
            }
        });
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(SettingActivity.this);
            }
        });
        clear_cache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MaterialDialog dialog = new MaterialDialog.Builder(SettingActivity.this).title("提示").content("已清除缓存").positiveText("确认").onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                }).build();
                dialog.show();
            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MaterialDialog dialog = new MaterialDialog.Builder(SettingActivity.this).title("提示").content("是否退出App").positiveText("确认").onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        ServiceFactory.GetInstance().createService(PersonInfoService.class).exitLoginStatus().compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response) {

                            }

                            @Override
                            public void onFailure(BaseBean response) {

                            }
                        });
                        JPushInterface.clearAllNotifications(SettingActivity.this);
                        FeedCirclePushMessageEntity event=new FeedCirclePushMessageEntity();
                        event.setFeed(false);
                        event.setCircle(false);
                        EventBus.getDefault().post(event);
                        App.GetInstance().finishActivity(SettingActivity.this);
                        ExitAppAction action=new ExitAppAction();
                        action.setExit(true);
                        RxBus.getDefault().post(action);
                        PreferenceUtils.SaveUserToken("");
                        PreferenceUtils.SaveUserIdHash("");
                        StartRefreshTagsAction action_unbind_jpush=new StartRefreshTagsAction();
                        EventBus.getDefault().post(action_unbind_jpush);
                    }
                }).negativeText("取消").onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                }).build();
                dialog.show();
            }
        });
    }

    @Override
    public void setupData() {

    }
}
